﻿@{
    ViewBag.Title = "同城活动管理";
}
<h3>同城活动列表</h3>
<div align="center">
    <hr />
    <table class="tb" style="padding-right:50px;">
        <tbody>
            <tr>
                <td></td>
                <td style="width:88px;">
                    <a href="/manage/activities/edit">添加活动</a> 
                </td>
            </tr>
        </tbody>
    </table>
    <hr />
    <table class="tb" cellspacing="0" cellpadding="4" border="0" style="color:#333333;">
	    <tbody>
            <tr style="color:Black;background-color:#E2F4FE;font-weight:bold;">
			    <th scope="col">序号</th>
                <th scope="col">前往</th>
                <th scope="col">标题</th>
                <th scope="col">图片</th>
                <th scope="col">描述</th>
                <th scope="col">管理</th>
                <th scope="col">排序</th>
		    </tr>
        </tbody>
        <tbody id ="tablist">
        </tbody>
    </table>
<hr />
<!--page-->
<div class="pagehtml pagination" style="text-align: center; margin-top:5px;"></div>
</div>
<script type="text/javascript">
    var pageIndex = 1;
    var pageSize = 10;
    var shtml;
    $(function () {
        GetList(pageIndex);
    });

    //get list data
    function GetList(_pageIndex) {
        pageIndex = _pageIndex;
        $.ajax({
            url: "/activities/get_list",
            type: "POST",
            dataType: "json",
            data: {
                page: pageIndex,
                pageSize: pageSize
            },
            timeout: 30000,
            success: function (data) {
                if (data.list.length > 0) {
                    $(".pagehtml").show();
                    createHTML(data.list);
                    $(".pagehtml").pagination(data.recordCount, { load_callback: false, callback: pageselectCallback, current_page: data.page, items_per_page: data.pageSize, link_to: "#" });
                }
                else {
                    $(".pagehtml").hide();
                    $("#tablist").html("<tr><td colspan='7' style='color:red;'>没有数据！</td></tr>");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                dlg.Msg.Err(errorThrown);
            }
        });
    }

    // page call back function
    function pageselectCallback(page_index) {
        GetList(page_index);
    }

    //create html
    function createHTML(data) {
        var html = "";
        $(data).each(function (i, item) {
            var _tr = '<tr id=' + item.ID + ' order=' + item.Order + ' align="center"  onmouseover="this.style.backgroundColor=\'#E2F4FE\'" onmouseout="this.style.backgroundColor=\'#ffffff\'" style="background-color: rgb(255, 255, 255); ">'
              + '<td>' + (pageSize * (pageIndex - 1) + (i + 1)) + '</td>'
              + '<td>' + item.Target + '</td>'
              + '<td>' + item.Title + '</td>'
              + '<td>' + item.Img + '</td>'
              + '<td>' + item.Description + '</td>'
              + '<td><a href="/manage/activities/edit/' + item.ID + '">编辑</a>\
                <a href="javascript:void(0);" onclick="del_activies(' + item.ID + ')">删除</a></td>'
              + '<td><a href="javascript:void(0)" class="goUp">上移</a>&nbsp;|&nbsp;<a href="javascript:void(0)" class="goDown">下移</a></td>'
              + '</tr>';
            html += _tr;
        });
        $("#tablist").html(html);

        //上移下移显示处理
        var len = $('.tb tr').length - 1;
        $('.tb .goUp,.tb .goDown').show();
        $('.tb tr').eq(2).find('.goUp').hide();
        $('.tb tr').eq(len).find('.goDown').hide();
    }

    //delete
    function del_activies(aid) {
        dlg.Confirm('提示', '确定要删除该项吗 ?', function () {
            $.getJSON("/activities/json_delete", { aid: aid }, function (data) {
                if (data) {
                    dlg.Msg.Cor("操作成功");
                    setTimeout("$('#' + " + aid + ").remove();", 500);
                }
                else {
                    dlg.Msg.Err("操作失败");
                }
            });
        });
    }

    // order up
    $('.tb').delegate('.goUp', "click", function () {
        var moveTr = $(this).closest('tr');

        var id = moveTr.attr("id");
        var num = moveTr.attr("order");
        var id2 = moveTr.prev().attr("id");
        var num2 = moveTr.prev().attr("order");

        if (num == num2) {
            num = parseInt(num) + 1;
        }
        $.getJSON("/album/json_order", { id: id, num: num, id2: id2, num2: num2 }, function (data) {
            if (data) {
                dlg.Msg.Cor("操作成功");
                setTimeout("GetList(" + pageIndex + ");", 500);
            }
            else {
                dlg.Msg.Err("操作失败");
            }
        });
    })

    // order down
    $('.tb').delegate('.goDown', "click", function () {
        var moveTr = $(this).closest('tr');

        var id = moveTr.attr("id");
        var num = moveTr.attr("order");
        var id2 = moveTr.next().attr("id");
        var num2 = moveTr.next().attr("order");

        if (num == num2) {
            num2 = parseInt(num2) + 1;
        }
        $.getJSON("/activities/json_order", { id: id, num: num, id2: id2, num2: num2 }, function (data) {
            if (data) {
                dlg.Msg.Cor("操作成功");
                setTimeout("GetList(" + pageIndex + ");", 500);
            }
            else {
                dlg.Msg.Err("操作失败");
            }
        });
    })
</script>
